<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * 定义主键字段
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 定义表名，不含前缀
     *
     * @var string
     */
    protected $table      = 'wd_user';

    /**
     * 定义拒绝保存的字段
     *
     * @var array
     */
    protected $guarded    = ['id'];

    /**
     * 开启laravel默认的时间记录
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 管理shop
     *
     * @return void
     */
    public function shop()
    {
        return $this->belongsTo('App\Models\Shop', 'shop_id', 'id');

    }

    /**
     * 获取用户的推广码 用于新用户邀请码使用
     *
     * @return value
     */
    public static function get_invite_code($user_id)
    {
        return 'UX'.$user_id;
    }

    /**
     * 获取用户是否实名认证
     *
     * @return value 0未实名认证 1已实名认证
     */
    public static function is_real_name($user_id)
    {
        $return = 0;

        if($user_id){

            $info = self::find($user_id);

            if($info && $info->real_name && $info->id_card){

                $return = 1;
            }

        }

        return $return;

    }

    /**
     * 生成密码
     *
     * @return value
     */
    public static function create_pwd($pwd)
    {
        $pwd_prefix = 'wlxm';
        $pwd = $pwd.$pwd_prefix;

        return md5($pwd);
    }
    
    /**
     * 验证密码
     *
     * @return json
     */
    public static function check_pwd($pwd, $user_pwd)
    {
        $pwd_prefix = 'wlxm';

        $pwd = $pwd.$pwd_prefix;

        return md5($pwd) == $user_pwd ? true : false;

    }

    /**
     * 登录成功后用手机号时间戳随机数
     * 生成token
     */
    public static function token($tel)
    {
        return md5(mt_rand().$tel.time());
    }

    /**
     * 设置昵称
     */
    public static function get_nickname($tel)
    {
        $nickname = '';

        if($tel){

            // $nickname = substr_replace($tel,'****', 3, 4);
            $nickname = '格主'.rand(100000,999999);

        }

        return $nickname;

    }

    /**
     * [get_user_info 组装用户登录返回的信息]
     * @param  [type] $userinfo [description]
     * @return [type]           [description]
     */
    public static function get_user_info($userinfo)
    {
        $info = [];

        if($userinfo){
            $info['id']              = $userinfo->id;
            $info['user_nickname']   = $userinfo->user_nickname;
            $info['user_mobile']     = $userinfo->user_mobile;
            $info['user_name']       = $userinfo->user_name;
            $info['login_time']      = $userinfo->login_time;
            $info['login_token']     = $userinfo->login_token;
            $info['user_avatar']     = $userinfo->user_avatar;
            $info['creditor_status'] = $userinfo->creditor_status;
            $info['is_pay_pwd']      = empty($userinfo->pay_pwd) ? 0 : 1;
        }

        return $info;
    }

    /**
     * [获取随机头像]
     * @param  [type] $userinfo [description]
     * @return [type]           [description]
     */
    public static function get_rand_avatar()
    {
        $key         = rand(1,28);
        
        $avatar_url  = 'http://public.gexinec.com/Head_pic/head_portrait/head_portrait';
        $avatar_pre  = '.png';

        $user_avatar = $avatar_url.$key.$avatar_pre;

        return $user_avatar;
    }
  
    /**
     * 组装余额界面返回数据
     *
     * @param [type] $userinfo
     * @return void
     */
    public static function get_user_balance($userinfo)
    {
        $info = [];

        if($userinfo){
            $info['id']            = $userinfo->id;
            $info['balance']       = $userinfo->balance;
            $info['share_gold']    = $userinfo->share_gold;
            $info['creditor']      = $userinfo->creditor;
        }

        return $info;
    }

   
    /**
     * 计算身份证校验码，根据国家标准GB 11643-1999
    *
     *
     * @param [type] $idcard_base
     * @return void
     */
    public static function idcard_verify_number($idcard_base){

        if (strlen($idcard_base) != 17){ return false; }
        // 加权因子
        $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);

        // 校验码对应值
        $verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');

        $checksum = 0;
        for ($i = 0; $i < strlen($idcard_base); $i++){
            $checksum += substr($idcard_base, $i, 1) * $factor[$i];
        }

        $mod = strtoupper($checksum % 11);
        $verify_number = $verify_number_list[$mod];

        return $verify_number;

    }

    /**
     * 将15位身份证升级到18位
     *
     * @param [type] $idcard
     * @return void
     */
    public static function idcard_15to18($idcard){

        if (strlen($idcard) != 15){
            return false;
        }else{
            // 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码
            if (array_search(substr($idcard, 12, 3), array('996', '997', '998', '999')) !== false){
                $idcard = substr($idcard, 0, 6) . '18'. substr($idcard, 6, 9);
            }else{
                $idcard = substr($idcard, 0, 6) . '19'. substr($idcard, 6, 9);
            }
        }

        $idcard = $idcard . self::idcard_verify_number($idcard);

        return $idcard;

    }

    /**
     * 18位身份证校验码有效性检查
     *
     * @param [type] $idcard
     * @return void
     */
    public static function idcard_checksum18($idcard){

        if (strlen($idcard) != 18){ return false; }

        $aCity = array(11 => "北京",12=>"天津",13=>"河北",14=>"山西",15=>"内蒙古",
        21=>"辽宁",22=>"吉林",23=>"黑龙江",
        31=>"上海",32=>"江苏",33=>"浙江",34=>"安徽",35=>"福建",36=>"江西",37=>"山东",
        41=>"河南",42=>"湖北",43=>"湖南",44=>"广东",45=>"广西",46=>"海南",
        50=>"重庆",51=>"四川",52=>"贵州",53=>"云南",54=>"西藏",
        61=>"陕西",62=>"甘肃",63=>"青海",64=>"宁夏",65=>"新疆",
        71=>"台湾",81=>"香港",82=>"澳门",
        91=>"国外");
        //非法地区
        if (!array_key_exists(substr($idcard,0,2),$aCity)) {
            return false;
        }
        //验证生日
        if (!checkdate(substr($idcard,10,2),substr($idcard,12,2),substr($idcard,6,4))) {
            return false;
        }
        $idcard_base = substr($idcard, 0, 17);
        if (self::idcard_verify_number($idcard_base) != strtoupper(substr($idcard, 17, 1))){
            return false;
        }else{
            return true;
        }
    }
}
